package com.qf.shop.user.service.impl;

import com.qf.shop.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.shop.common.utils.PageUtils;
import com.qf.shop.common.utils.Query;

import com.qf.shop.user.dao.UserDao;
import com.qf.shop.user.entity.UserEntity;
import com.qf.shop.user.service.UserService;


@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {


        // 1.创建分页对象
        IPage<UserEntity> page1 = new Query<UserEntity>().getPage(params);

        // 2.创建查询对象
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        Object username = params.get("username");
        if (username != null && !StringUtils.isEmpty(username.toString())) {
            queryWrapper.like("username", username);
        }

        // 3.组合条件查询
        IPage<UserEntity> page = this.page(page1, queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public UserVo queryUser(String name) {
        // 1.设置条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.
                eq(true, "email", name).
                or(true).
                eq(true, "username", name)
                .or(true)
                .eq(true, "phone", name);
        UserEntity userEntity = this.getOne(wrapper);

        if (userEntity != null) {
            userEntity.setPassword(null);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userEntity, userVo);
            return userVo;
        }
        return null;
    }

}